package com.puhui.lib.widgets

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import com.puhui.lib.R


/**
 * Description: 任意圆角控件
 * Data：2020/6/5-9:37
 * Author: TangJian
 */
class RoundCornerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : View(context, attrs) {
    // 圆角半径
    private var radius: Float = resources.getDimension(R.dimen.space_5)

    // 按钮正常时的背景颜色
    private var startColor: Int
    private var endColor: Int

    //背景边框
    private var isNeedFrame = false  //是否需要边框
    private var rect: RectF? = null
    private var frameColor: Int = 0xf6f6f6  //边框颜色
    private var frameWidth: Float = 1f  //边框宽度
    private var frameCornerRadius: Float = radius //边框圆角
    private lateinit var framePaint: Paint  //边框画笔

    init {
        val typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.RoundCornerView)
        radius =
            typedArray.getDimension(R.styleable.RoundCornerView_cornerRadius, resources.getDimension(R.dimen.space_5))
        frameCornerRadius = radius

        val defaultColor = ContextCompat.getColor(getContext(), R.color.bg_color_f6)
        startColor = typedArray.getColor(R.styleable.RoundCornerView_bgStartColor, defaultColor)
        endColor = typedArray.getColor(R.styleable.RoundCornerView_bgEndColor, 0)

        frameColor = typedArray.getColor(R.styleable.RoundCornerView_frameColor, defaultColor)
        frameWidth = typedArray.getDimension(R.styleable.RoundCornerView_frameWidth, 1f)
        isNeedFrame = typedArray.getBoolean(R.styleable.RoundCornerView_needFrame, false)

        typedArray.recycle()

        val temp = GradientDrawable.Orientation.LEFT_RIGHT
        if (endColor == 0) {
            val gradientDrawable = GradientDrawable()
            gradientDrawable.orientation = temp
            gradientDrawable.cornerRadius = radius
            gradientDrawable.setColor(startColor)
            background = gradientDrawable
        } else {
            //创建无长度数组： var result = arrayOf<Int>()    var result = intArrayOf()
            //创建已知长度数组： var result =  Array<Int>(10)    var result = IntArray(10)
            val gradientDrawable = GradientDrawable(temp, intArrayOf(startColor, endColor))
            gradientDrawable.cornerRadius = radius
            background = gradientDrawable
        }
    }

    fun setBackColor(@ColorInt startColor: Int, @ColorInt endColor: Int = 0) {
        val colors = if (endColor == 0) intArrayOf(startColor) else intArrayOf(startColor, endColor)

        val gradientDrawable = GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors)
        gradientDrawable.cornerRadius = radius
        gradientDrawable.setColor(startColor)
        background = gradientDrawable
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        if (isNeedFrame) {
            val temp = frameWidth
            rect = RectF(temp, temp, measuredWidth.toFloat() - temp, measuredHeight.toFloat() - temp)

            framePaint = Paint()
            framePaint.color = frameColor
            framePaint.strokeWidth = frameWidth
            framePaint.isAntiAlias = true
            framePaint.style = Paint.Style.STROKE

            if (frameCornerRadius > measuredHeight / 2f) {
                frameCornerRadius = measuredHeight / 2f
            }
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        if (isNeedFrame) {
            rect?.let {
                canvas?.drawRoundRect(it, frameCornerRadius, frameCornerRadius, framePaint)
            }
        }
    }
}